<?php
class TopatsAction extends TopAction {

	public function sold () {
		
		$this->display();
			
	}
	
	public function task () {
		set_time_limit(0);//无限请求超时时间      
 
	
		$task_id  = $_SESSION['task_id'];
		
		$i=0;      
		while (true){      
			sleep(10); 
			$i++; 
			###<<<<
			####get Task ####
			if( empty( $task_id ) ) {
				for( $i=0;$i<3;$i++) {
					$params = array(); 
					$params = array (
							'method' => 'taobao.topats.trades.sold.get',
							'session' => $this->_top_session,
							'timestamp' => date ( 'Y-m-d H:i:s' ),
							'format' => TAO_RETURN_FORMAT,
							'app_key' => TAO_APPKEY,
							'v' => '2.0',
							'sign_method' => 'md5',
							
							'start_time' => date('Ymd', strtotime('-2 day') ),
							'end_time' => date('Ymd', strtotime('-1 day') ),
							'fields' => 'seller_nick,buyer_nick,num,tid,buyer_message,payment,buyer_memo,seller_memo,receiver_name,receiver_state,receiver_city,receiver_district,receiver_address,receiver_zip,receiver_mobile,receiver_phone,orders,orders.pic_path,orders.sku_properties_name,orders.outer_sku_id,pic_path,seller_flag,status,created',
					);
					//print_r( $params );
					$tc = new TopClient ();
					
					$task = $tc->getResult ( $params );
					Log::Write( 'Task:::getTask'. $_SESSION['seller_nick'] .':::'. date('Y-m-d H:i:s') . ':::' . var_export( $task, true) , LOG::INFO , '', LOG_PATH.date('y_m_d').'.task.log');
					//echo '<pre>';
					//print_r( $task );
					if( $task['topats_trades_sold_get_response']['task'] ) {
						$task_id = $task['topats_trades_sold_get_response']['task']['task_id'] ;
					}else if( $task['error_response'] ){
						preg_match( '/\d+/', $task['error_response']['sub_msg'], $match );
						
						$task_id =  $match[0];
						$_SESSION['task_id'] = $task_id;
					}
					if(! empty( $task ) ) break;
				}
			}
				//echo '<a href="' .U('task?task_id='. $task_id ). '">查看任务</a>';
			
			
			### parse Task ###
			//if( empty( $task_id ) ) $this->error( '任务参数为空' );
			for( $i=0;$i<3;$i++) {
			$params = array (
					'method' => 'taobao.topats.result.get',
					'session' => $this->_top_session,
					'timestamp' => date ( 'Y-m-d H:i:s' ),
					'format' => TAO_RETURN_FORMAT,
					'app_key' => TAO_APPKEY,
					'v' => '2.0',
					'sign_method' => 'md5',
					
					'task_id' => $task_id
			);
			
			$tc = new TopClient ();
			
			$task = $tc->getResult ( $params );
			$taskRet = $task['topats_result_get_response']['task'] ;
			Log::Write( 'Task:::Ret:'. $_SESSION['seller_nick'] .':::'. date('Y-m-d H:i:s') . ':::' . var_export( $task, true) , LOG::INFO , '', LOG_PATH.date('y_m_d').'.task.log');
				if(! empty( $task ) ) break;
			
			}
			if( $taskRet ) {
				if( $taskRet['status'] == 'done' ) {
					echo $taskRet['download_url'] ;
					Log::Write( 'Task:::'. $_SESSION['seller_nick'] .':::'. date('Y-m-d H:i:s') . ':::' . var_export( $taskRet, true) , LOG::INFO , '', LOG_PATH.date('y_m_d').'.task.log');
					$contents = file_get_contents( $taskRet['download_url'] );
					
					$cache_key = 'sold_'.$_SESSION['seller_nick'];
					$zipfile = CACHE_PATH.$cache_key.'.zip' ;
					file_put_contents( $zipfile , $contents );
					
					$zip=new ZipArchive;//新建一个ZipArchive的对象
					if($zip->open( $zipfile )===TRUE){
					$zip->extractTo( CACHE_PATH );//假设解压缩到在当前路径下images文件夹内
					$zip->close();//关闭处理的zip文件
					}else{
						$this->error('parse ERROR');
					
					}
					$filestr = file_get_contents( CACHE_PATH.$taskRet['task_id'] );
					
					$this->ajaxReturn ( $filestr, 'Success', 1 ) ;
				
					//S ( $cache_key, $contents, 1800 );
					
				}else {
					$this->error('正在获取...任务ID:'.$task_id );
				}
			}
			
			#### end Parse ###
			
			//10秒内返回一个
			if($i==30){
				$this->error('正在获取...');
			}
	
	
			###>>>
		}
		
	
	}
	
}